package com.danco.training.zaneuskaya.connection;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateSessionUtil {

	private static final Logger logger = Logger
			.getLogger(HibernateSessionUtil.class);

	private static final SessionFactory sessionFactory = buildSessionFactory();
	 
	@SuppressWarnings("deprecation")
	private static SessionFactory buildSessionFactory() {
		try {
			
			SessionFactory sessionFactory = new Configuration().configure()
					.buildSessionFactory();
 
			return sessionFactory;
 
		} catch (Throwable ex) {
			logger.error(ex.getMessage(), ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
 
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
 
	public static void openSession(){
		getSessionFactory().openSession();
	}
	public static void shutdown() {
		getSessionFactory().getCurrentSession().close();
		getSessionFactory().close();
	}
 
}